var Calendar = function(c) {
	this.divId = c.RenderID ? c.RenderID : '[data-render="calendar"]', this.DaysOfWeek = c.DaysOfWeek ? c.DaysOfWeek : [
		"日", "一", "二", "三", "四", "五", "六"
	], this.Months = c.Months ? c.Months : ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月",
		"十二月"
	];
	var a = new Date;
	this.CurrentMonth = a.getMonth(), this.CurrentYear = a.getFullYear();
	var b = c.Format;
	this.f = "string" == typeof b ? b.charAt(0).toUpperCase() : "M"
};
Calendar.prototype.nextMonth = function() {
	11 == this.CurrentMonth ? (this.CurrentMonth = 0, this.CurrentYear = this.CurrentYear + 1) : this.CurrentMonth = this
		.CurrentMonth + 1, this.divId = '[data-active="false"] .render', this.showCurrent()
}, Calendar.prototype.prevMonth = function() {
	0 == this.CurrentMonth ? (this.CurrentMonth = 11, this.CurrentYear = this.CurrentYear - 1) : this.CurrentMonth = this
		.CurrentMonth - 1, this.divId = '[data-active="false"] .render', this.showCurrent()
}, Calendar.prototype.previousYear = function() {
	this.CurrentYear = this.CurrentYear - 1, this.showCurrent()
}, Calendar.prototype.nextYear = function() {
	this.CurrentYear = this.CurrentYear + 1, this.showCurrent()
}, Calendar.prototype.showCurrent = function() {
	this.Calendar(this.CurrentYear, this.CurrentMonth)
}, Calendar.prototype.checkActive = function() {
	1 == document.querySelector(".months").getAttribute("class").includes("active") ? document.querySelector(".months").setAttribute(
		"class", "months") : document.querySelector(".months").setAttribute("class", "months active"), "true" == document.querySelector(
		".month-a").getAttribute("data-active") ? (document.querySelector(".month-a").setAttribute("data-active", !1),
		document.querySelector(".month-b").setAttribute("data-active", !0)) : (document.querySelector(".month-a").setAttribute(
		"data-active", !0), document.querySelector(".month-b").setAttribute("data-active", !1)), setTimeout(function() {
		document.querySelector(".calendar .header").setAttribute("class", "header active")
	}, 200), document.querySelector("body").setAttribute("data-theme", this.Months[document.querySelector(
		'[data-active="true"] .render').getAttribute("data-month")].toLowerCase())
}, Calendar.prototype.Calendar = function(B, j) {
	"number" == typeof B && (this.CurrentYear = B), "number" == typeof B && (this.CurrentMonth = j);
	var z = (new Date).getDay()+7,
		x = (new Date).getMonth(),
		b = (new Date).getFullYear(),
		y = new Date(B, j, 1).getDay(),
		p = new Date(B, j + 1, 0).getDate(),
		C = 0 == j ? new Date(B - 1, 11, 0).getDate() : new Date(B, j, 0).getDate(),
		A = "<span>" + this.Months[j] + " &nbsp; " + B + "</span>",
		g = '<div class="table">';
	g += '<div class="row head">';
	for (var f = 0; f < 7; f++) {
		g += '<div class="cell">' + this.DaysOfWeek[f] + "</div>"
	}
	g += "</div>";
	for (var k, q = dm = "M" == this.f ? 1 : 0 == y ? -5 : 2, D = (f = 0, 0); D < 6; D++) {
		g += '<div class="row">';
		for (var w = 0; w < 7; w++) {
			if ((k = f + dm - y) < 1) {
				g += '<div class="cell disable">' + (C - y + q++) + "</div>"
			} else {
				if (k > p) {
					g += '<div class="cell disable">' + q++ + "</div>"
				} else {
					g += '<div class="cell' + (z == k && this.CurrentMonth == x && this.CurrentYear == b ? " active" : "") +
						'"><span>' + k + "</span></div>", q = 1
				}
			}
			f % 7 == 6 && k >= p && (D = 10), f++
		}
		g += "</div>"
	}
	g += "</div>", document.querySelector('[data-render="month-year"]').innerHTML = A, document.querySelector(this.divId)
		.innerHTML = g, document.querySelector(this.divId).setAttribute("data-date", this.Months[j] + " - " + B), document.querySelector(
			this.divId).setAttribute("data-month", j)
}, window.onload = function() {
	var b = new Calendar({
		RenderID: ".render-a",
		Format: "M"
	});
	b.showCurrent(), b.checkActive();
	var a = document.querySelectorAll(".header [data-action]");
	for (i = 0; i < a.length; i++) {
		a[i].onclick = function() {
			if (document.querySelector(".calendar .header").setAttribute("class", "header"), "true" == document.querySelector(
					".months").getAttribute("data-loading")) {
				return document.querySelector(".calendar .header").setAttribute("class", "header active"), !1
			}
			var c;
			document.querySelector(".months").setAttribute("data-loading", "true"), this.getAttribute("data-action").includes(
				"prev") ? (b.prevMonth(), c = "left") : (b.nextMonth(), c = "right"), b.checkActive(), document.querySelector(
				".months").setAttribute("data-flow", c), document.querySelector('.month[data-active="true"]').addEventListener(
				"webkitTransitionEnd",
				function() {
					document.querySelector(".months").removeAttribute("data-loading")
				}), document.querySelector('.month[data-active="true"]').addEventListener("transitionend", function() {
				document.querySelector(".months").removeAttribute("data-loading")
			})
		}
	}
};
